package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberDemandquotationBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.ui.fragment.DemandQuotationFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;

import java.util.ArrayList;

/**
 * 需求报价
 */
public class DemandQuotationActivity extends AppActivity {

    private ActivityMemberDemandquotationBinding mBind;
    private final String[] mTitles = {"商品报价", "租赁报价"};
    private final SparseArray<String> mSearchList = new SparseArray<>();
    private ArrayList<Fragment> mFragments;

    public static void start(Context context) {
        Intent intent = new Intent(context, DemandQuotationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_member_demandquotation);
        mFragments = new ArrayList<>();
        // 个人：没有商品需求报价
        boolean isPersonRole = MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_PERSONAL;
        if (!isPersonRole) {
            mFragments.add(DemandQuotationFragment.newInstance(0));
        }
        mFragments.add(DemandQuotationFragment.newInstance(1));
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBind.vp.setOffscreenPageLimit(mTitles.length);
        mBind.vp.setAdapter(mPagerAdapter);
        if (!isPersonRole) {
            mBind.tabLayout.setupWithViewPager(mBind.vp);
            mBind.tabLayout.setNormalColor(R.color.color_main_bg);
            mBind.tabLayout.setSelectColor(R.color.color_white);
            mBind.tabLayout.setNormalSize(15);
            mBind.tabLayout.setSelectSize(18);
            mBind.tabLayout.addOnTabSelectedListener(mTitles, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mBind.vp.setCurrentItem(tab.getPosition());
                }
            });
        } else {
            mBind.tabLayout.setVisibility(View.GONE);
        }
        initSearchView();
    }

    private String mSearchText = ""; // 搜索内容

    private void initSearchView() {
        mBind.activityBar.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        String hint = "请输入关键词，比如需求名称等";
        mBind.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBind.activityBar.etView.setHint(hint);
        mBind.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBind.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return true;
                    }
                    if (mSearchText.equals(mBind.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBind.activityBar.etView.getText().toString().trim();
                    loadSearchData();
                }
                return false;
            }
        });
        mBind.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBind.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBind.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBind.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBind.activityBar.etView.setText("");
                loadSearchData();
            }
        });
        mBind.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBind.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return;
                    }
                }
                if (mSearchText.equals(mBind.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBind.activityBar.etView.getText().toString().trim();
                loadSearchData();
            }
        });
        mBind.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    public String getSearchText() {
        int tabPosition = mBind.tabLayout.getSelectedTabPosition();
        return mSearchList.get(tabPosition);
    }

    private void loadSearchData() {
        int tabPosition = mBind.tabLayout.getSelectedTabPosition();
        mSearchList.put(tabPosition, mSearchText);
        DemandQuotationFragment fragment = (DemandQuotationFragment) mFragments.get(tabPosition);
        fragment.loadData(false);
    }

}
